package router

import (
	"background/http/api/dto"
	"background/http/api/helper"
	"background/model/back/entity"
	"background/model/back/srv"
	"background/model/constant"
	"background/utilx/ginx"
	"github.com/gin-gonic/gin"
)

type CloudLogTraffic struct {
	api srv.CloudLogTraffic
}

func NewCloudLogTraffic(api srv.CloudLogTraffic) CloudLogTraffic {
	return CloudLogTraffic{api}
}
func (p CloudLogTraffic) InitRouter(group *gin.RouterGroup) {
	gg := group.Group("log/traffic")
	// 关闭验证
	gg.POST("/add", p.InsertOne())
	gg.POST("/search", p.GetBySelf())
}

// ======================
func (p CloudLogTraffic) InsertOne() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec entity.CloudLogTraffic
		if ginx.BodyJsonWithErr[entity.CloudLogTraffic](ctx, &rec) {
			return
		}
		err := p.api.InsertOne(ctx.Request.Context(), rec)

		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessWithMsg(ctx, "success")
	}
}
func (p CloudLogTraffic) GetBySelf() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var rec dto.SearchWithPage[entity.CloudLogTraffic]
		if ginx.BodyJsonWithErr[dto.SearchWithPage[entity.CloudLogTraffic]](ctx, &rec) {
			return
		}

		var (
			data any
			err  error
		)
		forceRedis := ctx.GetBool(constant.MediaRedisForce)
		//isAdmin := ctx.GetBool(constant.MediaIsAdmin)
		data, err = p.api.GetBySelfCache(forceRedis, ctx.Request.Context(), rec)
		if err != nil {
			helper.ErrorWithMsg(ctx, 400, err.Error())
			return
		}
		helper.SuccessData(ctx, data)
		return
	}
}
